$(function() {
	initShelfTab();
	initDownloadTab();
	initListener();
	
	function getTableHeight() {
	    return $(window).height() - $('#pageTop').outerHeight(true) - 200;
	}
	
	function initListener() {
		$(window).resize(function () {
			$('#dataTable').bootstrapTable('resetView', {
	            height: getTableHeight()
	        });
			$('#downloadTable').bootstrapTable('resetView', {
	            height: getTableHeight()
	        });
	    });
	}

	function initDownloadTab() {
		// 表格
		$('#downloadTable').bootstrapTable({
			height: getTableHeight(),
			url: 'simples/collections',
			idField: 'id',
			showRefresh: true,
			toolbar: '#downloadTableToolbar',
			iconSize: 'outline',
			icons: {
				paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
	            paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
	            refresh: 'glyphicon-repeat',
				toggle: 'glyphicon-list-alt',
				columns: 'glyphicon-list',
	            detailOpen: 'glyphicon-plus icon-plus',
	            detailClose: 'glyphicon-minus icon-minus'
			},
			columns: [
				{
					field: 'id',
					title: 'ID',
					align: 'center',
					visible: false
				},
				{
					field: 'name',
					title: '报表名称',
					halign: 'center',
					align: 'left'
				},
				{
					field: 'operate',
					title: '操作',
					align: 'center',
					events: window.operateEvents = {
					        'click .download': function (e, value, row, index) {
					        	//download(row.id);
					        }
					},
					formatter: function(value, row, index) {
						return [
								'<a class="download" title="下载" href="simples/collections/' + row.id + '">',
								'<i class="glyphicon glyphicon-download"></i>',
								'</a>'
						       ].join('');
					}
				}
			]
		});
	}
	
	function initShelfTab() {
		$.ajax({
			url: 'simples/last',
			type: 'GET',
			dataType: 'json',
			contentType: "application/json; charset=utf-8",
			success: function(result) {
				if (result.success) {
					var last = result.data;
					$('#detectTime').text(last.detectTimeDisplay + ' (' + last.timeZoneDisplay + ')');
					$('#totalCount').text(last.totalCount);
					$('#summaryCount').text(last.summaryCount);
					$('#hashTotal').text(formatMoney(last.hashTotalReal, 2) + ' (' + formatMoney(last.hashTotalTheo, 2) + ')');
					$('#hashPassCount').text(last.hashPassCount);
					$('#hashPassRate').text(formatMoney(last.hashPassRate, 2) + '%');
					$('#hashUnpassCount').text(last.hashUnpassCount);
					$('#failureCount').text(last.failureCount);
				}
			}
		});
		
		// 表格
		$('#dataTable').bootstrapTable({
			height: getTableHeight(),
			url: 'simples/summaries',
			idField: 'id',
//			pagination: true,
//			sidePagination: 'client',
//			pageSize: 1000,
			showRefresh: true,
			showToggle: true,
			showColumns: true,
			toolbar: '#dataTableToolbar',
			iconSize: 'outline',
			icons: {
				paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
	            paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
	            refresh: 'glyphicon-repeat',
				toggle: 'glyphicon-list-alt',
				columns: 'glyphicon-list',
	            detailOpen: 'glyphicon-plus icon-plus',
	            detailClose: 'glyphicon-minus icon-minus'
			},
			rowStyle: function(row, index) {
				if (row.summaryCount == 0) {
					return {classes: 'danger'};
				}
				if (row.hashPassCount == row.summaryCount) {
					return {classes: 'success'};
				}
				return {};
			},
			columns: [
				{
					field: 'id',
					title: 'ID',
					align: 'center',
					visible: false
				},
				{
					field: 'minerShelfName',
					title: '机架名称',
					halign: 'center',
					align: 'left'
				},
				{
					field: 'totalCount',
					title: '矿机总数',
					halign: 'center',
					align: 'right'
				},
				{
					field: 'summaryCount',
					title: '检测到的矿机数',
					halign: 'center',
					align: 'right'
				},
				{
					field: 'failureCount',
					title: '未检测到的矿机数',
					halign: 'center',
					align: 'right',
					cellStyle: function(value, row, index) {
						if (value > 0) {
							return {classes: 'warning'};
						}
						return {};
					}
				},
				{
					field: 'hashPassCount',
					title: '算力达标数',
					halign: 'center',
					align: 'right'
				},
				{
					field: 'hashUnpassCount',
					title: '算力不达标数',
					halign: 'center',
					align: 'right',
					cellStyle: function(value, row, index) {
						if (value > 0) {
							return {classes: 'warning'};
						}
						return {};
					}
				},
				{
					field: 'hashPassRate',
					title: '算力合格率',
					halign: 'center',
					align: 'right',
					formatter: function(value, row, index) {
						return value + '%';
					}
				},
				{
					field: 'hashTotalTheo',
					title: '机架理论算力',
					halign: 'center',
					align: 'right'
				},
				{
					field: 'hashTotalReal',
					title: '机架实际算力',
					halign: 'center',
					align: 'right'
				}
			]
		});
	}
});